package com.aidex.biz.controller;

import com.aidex.biz.domain.CustomerDongtai;
import com.aidex.biz.domain.CustomerXiezu;
import com.aidex.biz.domain.enums.DongTaiTypeEnum;
import com.aidex.biz.service.CustomerDongtaiService;
import com.aidex.biz.service.CustomerXiezuService;
import com.aidex.biz.service.MessageInfoService;
import com.aidex.biz.service.XiezuService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.system.service.ISysUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户协助Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-11-29
 */
@RestController
@RequestMapping("/biz/bizCustomerXiezu")
public class CustomerXiezuController extends BaseController {
    @Autowired
    private CustomerXiezuService customerXiezuService;
    @Autowired
    private XiezuService xiezuService;
    @Autowired
    private CustomerDongtaiService customerDongtaiService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private MessageInfoService messageInfoService;

    /**
     * 查询客户协助列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(CustomerXiezu customerXiezu, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customerXiezu.setCompanyId(null);
        } else {
            customerXiezu.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        customerXiezu.setPage(new PageDomain(request, response));
        return R.data(customerXiezuService.findPage(customerXiezu));
    }

    /**
     * 查询我的协助列表
     */
    @GetMapping("/myXiezulist")
    public R<PageInfo> myXiezulist(CustomerXiezu customerXiezu, HttpServletRequest request, HttpServletResponse response) {
        customerXiezu.setXiezuUserId(SecurityUtils.getLoginUser().getUserId());
        customerXiezu.setPage(new PageDomain(request, response));
        return R.data(customerXiezuService.findMyXieZuList(customerXiezu));
    }

    /**
     * 获取客户协助详细信息
     */
    @GetMapping(value = "/{id}")
    public R<CustomerXiezu> detail(@PathVariable("id") String id) {
        return R.data(customerXiezuService.get(id));
    }

    /**
     * 新增客户协助
     */
    @Log(title = "客户协助", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated CustomerXiezu customerXiezu) {
        if (customerXiezu.getXiezuUsers().length == 0) {
            return R.fail("请选择用户");
        }
        customerXiezuService.xiezhuSingle(customerXiezu);
        return R.status(true);
    }

    /**
     * 修改客户协助
     */
    @Log(title = "客户协助", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated CustomerXiezu customerXiezu) {
        return R.status(customerXiezuService.save(customerXiezu));
    }

    /**
     * 批量新增客户协助
     */
    @Log(title = "批量新增客户协助", businessType = BusinessType.INSERT)
    @PostMapping("/multiAdd")
    public R multiAdd(@RequestBody @Validated CustomerXiezu customerXiezu) {
        if (null != customerXiezu.getCustomerIds() && null != customerXiezu.getXiezuUsers()) {
            customerXiezuService.xiezhuBatch(customerXiezu);
        } else {
            return R.fail("请选择客户和协助员工");
        }
        return R.status(true);
    }

    /**
     * 删除客户协助
     */
    @Log(title = "客户协助", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        if (ids.length == 1) {
            CustomerXiezu customerXiezu = customerXiezuService.get(ids[0]);
            if (null != customerXiezu) {
                SysUser sysUser = iSysUserService.get(customerXiezu.getXiezuUserId());
                if (customerXiezuService.deleteCustomerXiezuByIds(ids) > 0) {
                    CustomerDongtai customerDongtai = new CustomerDongtai();
                    customerDongtai.setCustomerId(customerXiezu.getCustomerId());
                    customerDongtai.setType(DongTaiTypeEnum.SHADOW.getStatus());
                    ;
                    customerDongtai.setContent(SecurityUtils.getLoginUser().getUser().getName() + "-删除协助人员-" + sysUser.getName() + ("0".equals(sysUser.getStatus()) ? "(在职)" : "(离职)"));
                    return R.status(customerDongtaiService.save(customerDongtai));
                }
            }

        }
        return R.fail("操作失败！");
    }

    /**
     * 获取最大编号
     */
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(customerXiezuService.findMaxSort(new CustomerXiezu()));
    }

    /**
     * 导出客户协助列表
     */
    @Log(title = "客户协助", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(CustomerXiezu customerXiezu) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customerXiezu.setCompanyId(null);
        } else {
            customerXiezu.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<CustomerXiezu> list = customerXiezuService.findList(customerXiezu);
        ExcelUtil<CustomerXiezu> util = new ExcelUtil<CustomerXiezu>(CustomerXiezu.class);
        return util.exportExcel(list, "客户协助数据");
    }

}
